# -*- coding: utf-8 -*-


import pymongo
import scrapy
from scrapy.http import Request
from crawler.spiders.bit.exchanges import Exchanges


class ExampleSpider(scrapy.Spider):
    name = 'bit'

    def __init__(self, name=None, **kwargs):
        super(ExampleSpider, self).__init__(name, **kwargs)

        # 连接mongodb
        client = pymongo.MongoClient('localhost', 27017)
        # client = pymongo.MongoClient('47.254.26.182', 27017)
        self.db = client.bt_news

    def start_requests(self):
        prefix = 'ex://'

        ef = Exchanges()
        for exchange in ef.exchanges:

            markets = ef.exchanges[exchange].load_markets()
            for market in markets.keys():
                url = prefix + exchange + '/ticker/' + market

                yield Request(
                    url = url,
                    dont_filter=True,
                    headers={
                        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like '
                                      'Gecko) Chrome/65.0.3325.181 Safari/537.36'
                    },
                    meta={
                        'market': market,
                        'exchange': exchange,
                        'object': ef.exchanges[exchange]
                    },
                    callback=self.parse,
                )

    def parse(self, response):
        # 交易对
        market = response.meta.get('market')

        # 交易所
        exchange = response.meta.get('exchange')

        # tickers
        tickers = (response.meta.get('object')).fetch_ticker('%s' % market)

        # 行情数据
        data_tickers = {
            'exchange': exchange,
            'market': market,
            'tickers': tickers,
        }

        # 入库mongo
        self.db.bt_coin_market.update_one({'exchange': exchange, 'market': market},{'$set': data_tickers}, upsert=True)
        # self.db.bt_coin_market.insert(data_tickers)

        # kline
        trades = (response.meta.get('object')).fetch_trades(market)
        data_trades = {
            'exchange': exchange,
            'market': market,
            'trades': trades,
        }
        self.db.bt_kline.update_one({'exchange': exchange, 'market': market},{'$set': data_trades}, upsert=True)
        # self.db.bt_kline.insert(data_trades)

        # 交易对
        data_market = {
            'exchange': exchange,
            'market': market
        }
        self.db.bt_transaction_pair.update_one({'exchange': exchange, 'market': market},{'$set': data_market}, upsert=True)
        # self.db.bt_transaction_pair.insert(data_market)